package com.redwood.report.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.redwood.report.ItemDecoration.SpaceItemDecoration;
import com.redwood.report.R;
import com.redwood.report.adapter.MainListAdapter;
import com.redwood.report.base.BaseActivity;
import com.redwood.report.common.Constant;
import com.redwood.report.entity.JianKongInfo;
import com.redwood.report.entity.JitaiBeen;
import com.redwood.report.entity.ServiceResult;
import com.redwood.report.net.Api;
import com.redwood.report.net.NetUtils;
import com.redwood.report.util.ToastUtils;
import com.redwood.report.widget.RingCustomView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener{

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_bar)
    RelativeLayout titleBar;
    @BindView(R.id.root_layout)
    LinearLayout rootLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    private RingCustomView rcvJiadonglv;
    private TextView tvJiadonglv;
    private RingCustomView rcvDaijilv;
    private TextView tvDaijilv;
    private RingCustomView rcvHegelv;
    private TextView tvHegelv;
    private RingCustomView rcvGuzhanglv;
    private TextView tvGuzhanglv;
    private BaseQuickAdapter homeAdapter;
    private LinearLayout ll_none;
    RingCustomView rcv_bulianglv;
    private TextView tv_bulianglv;
    RingCustomView rcv_baofeilv;
    private TextView tv_baofeilv;
    private TextView tv_onlinecount,tv_diajicount,tv_jinggaocount,tv_offcount;
    private int page = 1;
    private int totalpage = Integer.MAX_VALUE;
    public static String day="2018-12-10";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //弹出键盘滚动问题
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_main);
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
        initAdapter();
        // getUpdateAutoCheck();
    }

    private void initAdapter() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.addItemDecoration(new SpaceItemDecoration(getActivity()).setSpace(7f).setSpaceColor(getResources().getColor(R.color.blueColorAppKey)));
        homeAdapter = new MainListAdapter();
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent=new Intent(getActivity(), ReportActivity.class);
                intent.putExtra("title",((JianKongInfo.InfoBean)adapter.getData().get(position)).getMingcheng());
                intent.putExtra("bianhao",((JianKongInfo.InfoBean)adapter.getData().get(position)).getXitongbianhao());
                startActivity(intent);
            }
        });
        homeAdapter.setOnLoadMoreListener(this, rvList);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_head, null);
        ll_none = view.findViewById(R.id.ll_none);
        tvJiadonglv = view.findViewById(R.id.tv_jiadonglv);
        rcvJiadonglv = view.findViewById(R.id.rcv_jiadonglv);
        tvDaijilv = view.findViewById(R.id.tv_daijilv);
        rcvDaijilv = view.findViewById(R.id.rcv_daijilv);
        tvHegelv = view.findViewById(R.id.tv_hegelv);
        rcvHegelv = view.findViewById(R.id.rcv_hegelv);
        tvGuzhanglv = view.findViewById(R.id.tv_guzhanglv);
        rcvGuzhanglv = view.findViewById(R.id.rcv_guzhanglv);
        rcv_bulianglv = view.findViewById(R.id.rcv_bulianglv);
        tv_bulianglv = view.findViewById(R.id.tv_bulianglv);
        rcv_baofeilv = view.findViewById(R.id.rcv_baofeilv);
        tv_baofeilv = view.findViewById(R.id.tv_baofeilv);
        tv_onlinecount= view.findViewById(R.id.tv_onlinecount);
        tv_diajicount= view.findViewById(R.id.tv_diajicount);
        tv_jinggaocount= view.findViewById(R.id.tv_jinggaocount);
        tv_offcount= view.findViewById(R.id.tv_offcount);
        homeAdapter.addHeaderView(view);
        rvList.setAdapter(homeAdapter);
        onRefresh();

    }

    private void setRCV(TextView tvbaofenbi, RingCustomView rcvView, float baifenbi) {
        rcvView.setProgress((int) (baifenbi * 1000));
        tvbaofenbi.setText(((int) (baifenbi * 1000))/10f + "%");
        //rc
    }

    @Override
    public void onRefresh() {
        page = 1;
        totalpage = Integer.MAX_VALUE;
        loadByServer(false);
    }
    @Override
    public void onLoadMoreRequested() {
        refreshLayout.setEnabled(false);
        if (page > totalpage) {
            refreshLayout.setEnabled(true);
            homeAdapter.loadMoreEnd(false);
            return;
        } else {
            if (homeAdapter.getData().size() < Constant.PAGE_SIZE) {
                homeAdapter.loadMoreEnd(true);
            } else {
                loadByServer(true);


            }
        }
    }
    private void loadByServer(final boolean isLoadMore) {

        Api.jiankongList(getActivity(), Constant.PAGE_SIZE, page, day, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if(rspData.getRecode()==1){
                    JianKongInfo jianKongInfo= (JianKongInfo) rspData;
                    if(jianKongInfo.getInfo()!=null&&jianKongInfo.getInfo().size()>0){
                        ll_none.setVisibility(View.GONE);
                    }else{
                        ll_none.setVisibility(View.VISIBLE);
                    }
                    if (page == 1) {
                        homeAdapter.setNewData(jianKongInfo.getInfo());
                        setTopUI(jianKongInfo.getTop());
                    } else {
                        homeAdapter.addData(jianKongInfo.getInfo());
                    }
                    totalpage = (jianKongInfo.getAllcount() + Constant.PAGE_SIZE - 1) / Constant.PAGE_SIZE;
                    page++;
                    if (isLoadMore) {
                        refreshLayout.setEnabled(true);
                        homeAdapter.loadMoreComplete();
                    } else {
                        refreshLayout.setRefreshing(false);
                    }
                }
            }

            @Override
            public void failed(String msg) {
                ToastUtils.show(msg);
            }

            @Override
            public void onUsedCache(String msg) {

            }
        },JianKongInfo.class);
    }

    private void setTopUI(JianKongInfo.TopBean topBean) {

        tv_onlinecount.setText( topBean.getZaixian()+"台");
        tv_diajicount.setText( topBean.getDaiji()+"台");
        tv_jinggaocount.setText( topBean.getJinggao()+"台");
        tv_offcount.setText( topBean.getLixian()+"台");
        setRCV(tvJiadonglv, rcvJiadonglv, (float)topBean.getJiadonglv());
        setRCV(tvDaijilv, rcvDaijilv, (float)topBean.getDaijilv());
        setRCV(tvHegelv, rcvHegelv, (float)topBean.getHegelv());
        setRCV(tvGuzhanglv, rcvGuzhanglv, (float)topBean.getGuzhanglv());
        setRCV(tv_bulianglv, rcv_bulianglv, (float)topBean.getBulianglv());
        setRCV(tv_baofeilv, rcv_baofeilv, (float)topBean.getBaofeilv());
    }


    private void initData() {


    }

    private void initView() {


    }


}
